package org.lion.jingbeifang_interviewquestions.calculatePrice.service;

import org.lion.jingbeifang_interviewquestions.calculatePrice.entity.Goods;
import org.lion.jingbeifang_interviewquestions.calculatePrice.entity.StrategyDiscount;
import org.lion.jingbeifang_interviewquestions.calculatePrice.entity.StrategySubtract;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class CalculateWithSubtract {

    public BigDecimal calculateGoodsPrice(List<Goods> goodsList, StrategySubtract strategySubtract){
        BigDecimal x = strategySubtract.getX();
        BigDecimal y = strategySubtract.getY();
        BigDecimal totalPrice = BigDecimal.ZERO;

        for (Goods goods : goodsList){
            BigDecimal price = goods.getGoodsItem().getPrice();
            if (price != null){
                BigDecimal itemPrice = price.multiply(BigDecimal.valueOf(goods.getWeight())).multiply(BigDecimal.valueOf(goods.getDiscount().getDiscount()));
                totalPrice = totalPrice.add(itemPrice);
            }
        }
        // 不满足最低满减约束，则按照原价
        if (totalPrice.compareTo(x) < 0) {
            return totalPrice;
        }

        BigDecimal cycles = totalPrice.divide(x, 0, RoundingMode.DOWN);
        // 折扣价格
        BigDecimal deductionPrice = totalPrice.subtract(cycles.multiply(y));
        return deductionPrice;
    }

}
